package base.jdk8_demo.base64;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.UUID;

import org.junit.Test;

/**
 * jdk8 提供的 Base64 工具
 *
 */
public class Demo01 {

    @Test
    public void test(){
//      String s = "周";
		String s = "sann";
        for(int i=0; i<s.toCharArray().length; i++) {
            System.out.print(s.toCharArray()[i]+"\t");
        }
        System.out.println();

        try {
            byte[] bytes = s.getBytes("utf-8");
            for(int i=0; i< bytes.length; i++) {
                System.out.print(bytes[i]+"\t");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println();

        try {
            Base64.Encoder encoder = Base64.getEncoder();
            // 先将字符串s编码
            byte[] encoderBytes = encoder.encode(s.getBytes("utf-8"));
            for(int i=0; i<encoderBytes.length; i++) {
                System.out.print(encoderBytes[i]+"\t");
            }
            System.out.println();
            // 将s编码后的内容再次进行编码成字符串
            String encodeStr = encoder.encodeToString(encoderBytes);
            System.out.println("Base64 编码字符串:"+ encodeStr);
            System.out.println();

            Base64.Decoder decoder = Base64.getDecoder();
            byte[] decoderBytes = decoder.decode(encoderBytes);
            for(int i=0; i<decoderBytes.length; i++) {
                System.out.print(decoderBytes[i]+"\t");
            }
            System.out.println();

            System.out.println("Base64 解码字符串:"+ new String(decoderBytes,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        try {
            // 编码成字符串
            String base64EncodeStr = Base64.getEncoder().encodeToString("sann".getBytes("UTF-8"));
            System.out.println("sann字节长度为："+ "sann".getBytes("UTF-8").length);
            System.out.println("base64EncodeStr长度为："+base64EncodeStr);
            // 解码还原
            byte[] base64DecodeBytes = Base64.getDecoder().decode(base64EncodeStr);
            System.out.println("base64DecodeBytes长度为："+base64DecodeBytes.length);
            String s1 = new String(base64DecodeBytes, "utf-8");
            System.out.println("解码还原的字符串为："+s1);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {
        try {
            String url = "https://www.baidu.com?java8";
            // 编码URL字符串
            String base64EncodeStr = Base64.getUrlEncoder().encodeToString(url.getBytes());
            System.out.println("url字节长度为："+ url.getBytes("UTF-8").length);
            System.out.println("base64EncodeStr长度为："+base64EncodeStr);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 10; ++i) {
                String uu = UUID.randomUUID().toString();
                stringBuilder.append(uu);
            }
            System.out.println(stringBuilder.toString());

            byte[] mimeBytes = stringBuilder.toString().getBytes("UTF-8");

            String base64EncodeStr = Base64.getMimeEncoder().encodeToString(mimeBytes);
            System.out.println("mime字节长度为："+ mimeBytes.length);
            System.out.println("base64EncodeStr长度为："+base64EncodeStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        try {
            String s = "98bcd5ad-671f-45fc-bcc2-a0212fb89440";
            String s1 = Base64.getEncoder().encodeToString(s.getBytes("UTF-8"));
            System.out.println(s1);

            String s2 = Base64.getMimeEncoder().encodeToString(s.getBytes("UTF-8"));
            System.out.println(s2);

            String s3 = Base64.getUrlEncoder().encodeToString(s.getBytes("UTF-8"));
            System.out.println(s3);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
